package lx.exalarm.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import lx.exalarm.R;
import lx.exalarm.alarm.ActionBean;
import lx.exalarm.alarm.AlarmUtils;
import lx.exalarm.app.ThisApp;
import lx.exalarm.utils.HomeWatcher;
import lx.exalarm.utils.log.Log;

/**
 * Created by liuxu on 15-3-4.
 */
public abstract class BasePlayActivity extends BaseActivity
        implements BaseActivity.ActionBarCallbacks, HomeWatcher.OnHomePressedListener {

    private static final long mScheduleStopTime = 10 * 60 * 1000;

    protected static final String EXTRA_ACTION = "play_activity_extra_action";

    protected ActionBean mAction;
    protected ActionBean mNext;

    private HomeWatcher mHomeWatcher;

    public static void start(Class cls, ActionBean action) {
        Context cxt = ThisApp.getInstance();
        Intent intent = new Intent(cxt, cls);
        intent.putExtra(EXTRA_ACTION, action);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        cxt.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAction = (ActionBean) getIntent().getSerializableExtra(EXTRA_ACTION);
        Log.d(getClass().getSimpleName() + ".onCreate(), action=" + mAction);
        if (mAction == null) {
            finish();
            return;
        }
        mNext = mAction.getNextEnabledAction();

        final Window window = getWindow();
        window.addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        window.addFlags(
                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);

        initView(mAction, mNext);
        initHomeWatch();
        scheduleStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopHomeWatch();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        ActionBean action = (ActionBean) intent.getSerializableExtra(EXTRA_ACTION);
        if (action == null) {
            return;
        }
        ActionBean next = action.getNextEnabledAction();
        dataChanged(action, next);
    }

    @Override
    public void onBackPressed() {
        Toast.makeText(this, R.string.toast_back_key_no_use, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCreateActionBar(ImageView back, ImageView menu, TextView title) {
        back.setVisibility(View.INVISIBLE);
        menu.setVisibility(View.INVISIBLE);
    }

    @Override
    public boolean onActionBarBackClicked(View back) {
        return false;
    }

    @Override
    public boolean onActionBarMenuClicked(View menu) {
        return false;
    }

    protected void startNextAction() {
        if (mNext == null) {
            return;
        }
        AlarmUtils.startAction(mNext);
    }

    // we should be able to stop self if user is not there
    private void scheduleStop() {
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                finish();
            }
        }, mScheduleStopTime);
    }

    // watch home key press event
    private void initHomeWatch() {
        this.mHomeWatcher = new HomeWatcher(this);
        this.mHomeWatcher.setOnHomePressedListener(this);
        this.mHomeWatcher.startWatch();
    }

    private void stopHomeWatch() {
        this.mHomeWatcher.stopWatch();
    }

    @Override
    public void onHomePressed() {
        startNextAction();
    }

    @Override
    public void onHomeLongPressed() {
        // do nothing
    }

    protected abstract void initView(ActionBean action, ActionBean next);

    protected abstract void dataChanged(ActionBean action, ActionBean next);
}
